/* (c)2010-2011 Andrea Caltroni (andrea.caltroni@pd.infn.it), INFN (www.infn.it)
 *
 * This file is part of the IGI Survey Portlet (from here on shortened to IGISP).
 * IGISP is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) any 
 * later version.
 *
 * IGISP is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with IGISP. If not, see <http://www.gnu.org/licenses/>.
 */
package it.infn.grid.liferay.survey.modules.admin.web;

import java.beans.PropertyEditorSupport;

import org.ajm.spring.survey.modules.admin.model.Answer;
import org.ajm.spring.survey.modules.admin.service.AnswerService;

/**
 * Transforms an Answer ID into an Answer object and vice versa.
 * 
 * @author Andrea Caltroni, andrea.caltroni@pd.infn.it, INFN
 */
public class AnswerPropertyEditor extends PropertyEditorSupport {
    
    private AnswerService answerService;
    
    // ------------------------------------------------------------------------|

    /**
     * @param answerService
     */
    public AnswerPropertyEditor(AnswerService answerService) {
        
        super();
        this.answerService = answerService;
    }

    // ------------------------------------------------------------------------|

    /**
     * From form to model object.
     * Sets the property value by parsing a given String.
     */
    @Override
    public void setAsText(String answerId) {
        
        Answer ans = getAnswerService().findById(Long.parseLong(answerId));
        setValue(ans);
    }

    // ------------------------------------------------------------------------|

    /**
     * From model object to form.
     * Gets the property value as text.
     */
    @Override
    public String getAsText() {
        
        if(getValue() == null) {
            return null;
        }
        
        Answer ans = (Answer)getValue();
        return ans.getId().toString();
    }

    // ------------------------------------------------------------------------|

    /**
     * @return the answerService
     */
    public AnswerService getAnswerService() {
        
        return answerService;
    }

    // ------------------------------------------------------------------------|

    /**
     * @param answerService the answerService to set
     */
    public void setAnswerService(AnswerService answerService) {
        
        this.answerService = answerService;
    }
    
    // ------------------------------------------------------------------------|

}
